**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/create_outcomes.log, text replace
set more off

**Create outcome lists by source file
local arrests_list arrests serious type1_arrests
local complaints_list complaints force_complaints
local complaints_list complaints force_complaints
local trr_list force  
local awards_list merit merit_valor merit_valor_routine 
local yearly_list unit_change12 promoted12
local attend_list attend desk tact special district

*****************
**BRING IN CASE STATUS + COMPLAINTS DATA AND APPLY DATA RESTRICTION
*****************  
do ${rep_root}/code/data_restrict.do

**Extract incident yearmonth from each complaint
gen start_yr = year(inc_complaint_dt_1)
gen start_mo = month(inc_complaint_dt_1)
gen start_yrm = ym(start_yr, start_mo)

**Extract close time and closed yearmonth from each complaint
gen close_time = inc_closed_dt_1 - inc_start_dt_1
//gen close_yr = year(inc_closed_dt_1)
gen close_mo = month(inc_closed_dt_1)
gen close_yrm = ym(close_yr, close_mo)

**Kill close time if year is listed as 1999 (this is a placeholder in the original system)
replace close_time = . if close_yr == 1999

**Impute close date as the median close time after the start date
egen close_imp = median(close_time)  
replace inc_closed_dt_1 = inc_complaint_dt_1 + close_imp if missing(inc_closed_dt_1) | close_yr == 1999 //for some cases in 2007 and 2008 the data team put 1999 as a dummy year


*****************
**COMBINE WITH OUTCOME PANEL 
*****************  

**Create cartesian product with outcome panel. Each officer now has an observation for 
**every yearmonth of their career for every complaint lodged against them
keep cr_id acc_id mid start_yrm close_yrm inc_complaint_dt_1
joinby mid using ${rep_root}/data/all_panel
 
*****************
**CREATE AVERAGE OUTCOMES FOR DIFFERENT PERIODS
*****************  

**Define several periods relative to complaint start and close date
gen pre = inrange(yearmo, start_yrm - 12, start_yrm-1)
 

gen prey3 = inrange(yearmo, start_yrm - 36, start_yrm-25)
gen prey2 = inrange(yearmo, start_yrm - 24, start_yrm-13)
gen prey1 = inrange(yearmo, start_yrm - 12, start_yrm-1)
gen posty1 = inrange(yearmo, start_yrm+1, start_yrm + 12)
gen posty2 = inrange(yearmo, start_yrm+13, start_yrm + 24)
gen posty3 = inrange(yearmo, start_yrm+25, start_yrm + 36)
 

 
forval i = 1/36{
gen pre`i' = yearmo == start_yrm -(`i')
}
 
forval i = 0/36{
gen postc`i' = yearmo== start_yrm +(`i')
}

gen prec1_3 = inrange(yearmo, start_yrm-3, start_yrm - 1)
gen prec4_12 = inrange(yearmo, start_yrm-12, start_yrm - 4) 
 
gen postc1_3 = inrange(yearmo, start_yrm+1, start_yrm + 3)
gen postc4_12 = inrange(yearmo, start_yrm+4, start_yrm + 12)

 
**Adjust for the triggering complaint
foreach var in `complaints_list'{
	replace `var' = `var' - 1 if `var'>0 & yearmo==start_yrm
}

**For each  outcome, calculate the average outome over each period
sort cr_id acc_id
qui foreach var in `arrests_list' `complaints_list' `trr_list' `awards_list' `attend_list'{
	
	foreach stem in pre postcc prey3 prey2 prey1 posty1 posty2 posty3 prec1_3 prec4_12 postc1_3 postc4_12 pred postd{
	
	gen `var'_`stem'_i = .
	replace `var'_`stem'_i = `var' if `stem'==1
	by cr_id acc_id: egen `var'_`stem' = mean(`var'_`stem'_i)
	drop `var'_`stem'_i
	}
  
	forval n = 1/36{
	gen `var'_pre`n'_i = .
	replace `var'_pre`n'_i = `var' if pre`n'==1
	by cr_id acc_id: egen `var'_pre`n' = mean(`var'_pre`n'_i)
	drop `var'_pre`n'_i
	}
	
 
	forval n = 0/36{
	gen `var'_postc`n'_i = .
	replace `var'_postc`n'_i = `var' if postc`n'==1
	by cr_id acc_id: egen `var'_postc`n' = mean(`var'_postc`n'_i)
	drop `var'_postc`n'_i
	}
	
	 
	
}

 

**Mark if any complaints in any yearmonths prior to the start yrm for this complaint
gen any_prior_complaint_i = .
replace any_prior_complaint_i =  complaints>0 & yearmo<start_yrm & !missing(complaints) & !missing(start_yrm) 
by cr_id acc_id: egen any_prior_complaint = max(any_prior_complaint_i)
drop any_prior_complaint_i

**Keep one observation per complaint
by cr_id acc_id: keep if _n==1

**Flag complaints with listed close dates before their listed start dates
gen qflag_close_before_start = close_yrm < start_yrm & !missing(start_yrm)

 
//merge in profiles to see if officer resigned
merge m:1 mid using ${rep_root}/data/FOIA/FOIA_unified/profiles
drop if _merge == 2
drop _merge
 
**Calculate tenure in years at time of complaint
gen tenure = (inc_complaint_dt_1 - mdy(appoint_m, appoint_d, appoint_yr))/365.25

 **Extract resignation yearmo
gen resig_date = resignationdate
gen resig_date2 = date(resig_date, "DMY", 2023)
gen resig_yearmo = ym(year(resig_date2), month(resig_date2))
format %tm resig_yearmo

**Create indicator for officer resigning in post period
gen resigned_post = 0 if resig_date!= "NA"
replace resigned_post = 1 if close_yrm+13>resig_yearmo & resig_yearmo!=.
replace resigned_post = . if close_yrm == . 

**Repeat for alternative post periods
gen resigned_postc = 0 if resig_date!= "NA"
replace resigned_postc = 1 if start_yrm+13>resig_yearmo & resig_yearmo!=.
replace resigned_postc = . if start_yrm == . 

gen resigned_posth = 0 if resig_date!= "NA"
replace resigned_posth = 1 if close_yrm+7>resig_yearmo & resig_yearmo!=.
replace resigned_posth = . if close_yrm == . 

gen resigned_post2 = 0 if resig_date!= "NA"
replace resigned_post2 = 1 if close_yrm+25>resig_yearmo & resig_yearmo!=.
replace resigned_post2 = . if close_yrm == . 

gen resigned_post3 = 0 if resig_date!= "NA"
replace resigned_post3 = 1 if close_yrm+37>resig_yearmo & resig_yearmo!=.
replace resigned_post3 = . if close_yrm == . 


keep cr_id acc_id *_pre *_pre* *_post* qflag_close_before_start any_prior_complaint tenure

**If there's no observation of an officer in the post period (perhaps due to
**resignation or termination), they can't have been doing any officer-y things
foreach var of varlist *_post* *_pre*{
	
	replace `var' = 0 if `var' == .
}

 
save ${rep_root}/data/outcomes, replace
clear
log close

